package com.vip.buycart.dao.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.vip.buycart.dao.ProductDAO;
import com.vip.buycart.entity.Product;
import com.vip.buycart.exception.RecordNotFoundException;
import com.vip.framework.generic.GenericDaoImpl;

public class ProductDAOImpl extends GenericDaoImpl<Product> 
    implements ProductDAO {

    public ProductDAOImpl(Product t) {
        super(t);
    }

    /**
     * 分页查询商品方法
     * 
     * @param con
     *            查询条件，条件键表：productName,productDesc。
     * @param page
     *            页码
     * @param rowsPerPage
     *            每页记录数
     * @return 符合条件的商品集合
     */
    @Override
    public List<Product> search(HashMap<String, String> con, int page,int rowsPerPage) {
        StringBuilder hql = new StringBuilder();
        hql.append(" from Product p where 1=1 ");
        if (null != con.get("productName") && !"".equals(con.get("productName"))) {
            hql.append(" and p.productName = ").append(con.get("productName"));
        }
        if (null != con.get("productDesc") && !"".equals(con.get("productDesc"))) {
            hql.append(" and p.productDesc like '%").append(con.get("productDesc")).append("%'");
        }
        // TODO :改成按更新时间降序排列
        return pageListQuery(hql.toString(), page, rowsPerPage);
    }

    /**
     * 根据一个或多个id 查询数据库获取对应商品数据，返回一个商品的集合。
     * 
     * @param ids id数组。
     * @return
     * @throws RecordNotFoundException
     */
    public List<Product> getProductByIds(Long... ids)
            throws RecordNotFoundException {
        if(null==ids||ids.length==0)
            return null;
        StringBuilder hql = new StringBuilder(" from Product p where p.id in (");
        for (int i = 0; i < ids.length; i++) {
            hql.append(ids[i]);
            if(i!=ids.length-1)
                hql.append(",");
        }
        hql.append(")");
        List<Product> products=(List<Product>)this.executeHQL(hql.toString());
        
        //检查是否所以id对应的数据都能找到。
        if(null!=products&&products.size()!=ids.length){
            HashMap<Long,Object> goodIds=new HashMap<Long,Object>();
            for (Iterator<Product> iterator = products.iterator(); iterator.hasNext();) {
                Product product =iterator.next();
                goodIds.put(product.getId(), null);
            }
            Long[] badIds=new Long[ids.length];
            for (int i = 0,j=0; i < ids.length; i++) {
                if(!goodIds.containsKey(ids[i])){
                    badIds[j]=ids[i];
                    j++;
                }
            }
            logger.info("product not found ! ids:"+badIds);
            throw new RecordNotFoundException("product.not.found",badIds);
        }else if(null==products){
            logger.info("product not found ! ids:"+ids);
            throw new RecordNotFoundException("product.not.found",ids);
        }
        
        return products;
    }

    public Product getProductById(Long id) throws RecordNotFoundException {
        Product product=get(id);
        if(null==product){
            logger.info("product not found ! id:"+id);
            Long[] ids=new Long[1];
            ids[0]=id;
            throw new RecordNotFoundException("product.not.found",ids);
        }
        return product;
    }


	/**
	 * 通过商品类别Id 分页查询当前页码的数据
	 * @param id 
	 * @param page 页码
	 * @param rows 每页记录数
	 * @return 符合条件的商品集合
	 */
	public List<Product> getProductByPCID(Long id,int page ,int rows)
	{
		StringBuffer sql = new StringBuffer();
		sql.append(" from Product p where 1=1 ");
		if(id != -1)
		{
			sql.append(" and  p.productCategory = "+id);	
		}
		return pageListQuery(sql.toString(), page, rows);
	}
	/**
	 * 通过类别ID 得到该类别的总条数
	 * @param id
	 * @return
	 */
	public int getProductImageCount(Long id) {
		StringBuffer sql = new StringBuffer();
		sql.append(" FROM Product P WHERE P.productCategory = " + id);
		return this.countHQL(sql.toString());
	}
}
